package com.such.kit;

import org.slf4j.LoggerFactory;
import org.slf4j.spi.LocationAwareLogger;
import com.such.kit.validate.ValidateCommons;

/**
 * @author SUCH
 * <pre>
 * 日志记录器
 * 基于 slf4j 实现
 * </pre>
 */
public class Logger {

	public static void trace(Class<?> clazz, Object message) {
		trace(clazz, message, null);
	}

	public static void trace(Class<?> clazz, Object message, Throwable e) {
		writeLog(clazz, message, e, LocationAwareLogger.TRACE_INT);
	}

	public static void debug(Class<?> clazz, Object message) {
		debug(clazz, message, null);
	}

	public static void debug(Class<?> clazz, Object message, Throwable e) {
		writeLog(clazz, message, e, LocationAwareLogger.DEBUG_INT);
	}

	public static void info(Class<?> clazz, Object message) {
		info(clazz, message, null);
	}

	public static void info(Class<?> clazz, Object message, Throwable e) {
		writeLog(clazz, message, e, LocationAwareLogger.INFO_INT);
	}

	public static void warn(Class<?> clazz, Object message) {
		warn(clazz, message, null);
	}

	public static void warn(Class<?> clazz, Object message, Throwable e) {
		writeLog(clazz, message, e, LocationAwareLogger.WARN_INT);
	}

	public static void error(Class<?> clazz, Object message) {
		error(clazz, message, null);
	}

	public static void error(Class<?> clazz, Object message, Throwable e) {
		writeLog(clazz, message, e, LocationAwareLogger.ERROR_INT);
	}

	public static void fatal(Class<?> clazz, Object message) {
		fatal(clazz, message, null);
	}

	public static void fatal(Class<?> clazz, Object message, Throwable e) {
		writeLog(clazz, message, e, LocationAwareLogger.ERROR_INT);
	}

	public static boolean isTraceEnabled(Class<?> clazz) {
		return isEnabled(clazz, LocationAwareLogger.TRACE_INT);
	}

	public static boolean isDebugEnabled(Class<?> clazz) {
		return isEnabled(clazz, LocationAwareLogger.DEBUG_INT);
	}

	public static boolean isInfoEnabled(Class<?> clazz) {
		return isEnabled(clazz, LocationAwareLogger.INFO_INT);
	}

	public static boolean isWarnEnabled(Class<?> clazz) {
		return isEnabled(clazz, LocationAwareLogger.WARN_INT);
	}

	public static boolean isErrorEnabled(Class<?> clazz) {
		return isEnabled(clazz, LocationAwareLogger.ERROR_INT);
	}

	public static void writeLog(Class<?> clazz, Object message, Throwable e, int level) {
		if (ValidateCommons.isEmpty(message)) {
			message = "";
		}
		writeLog(clazz, message.toString(), e, level);
	}

	public static void writeLog(Class<?> clazz, String message, Throwable e, int level) {
		org.slf4j.Logger logger = getLogger(clazz);
		if (LocationAwareLogger.TRACE_INT == level) {
			if (logger.isTraceEnabled()) {
				logger.trace(message, e);
			}
		} else if (LocationAwareLogger.DEBUG_INT == level) {
			if (logger.isDebugEnabled()) {
				logger.debug(message, e);
			}
		} else if (LocationAwareLogger.INFO_INT == level) {
			if (logger.isInfoEnabled()) {
				logger.info(message, e);
			}
		} else if (LocationAwareLogger.WARN_INT == level) {
			if (logger.isWarnEnabled()) {
				logger.warn(message, e);
			}
		} else if (LocationAwareLogger.ERROR_INT == level) {
			if (logger.isErrorEnabled()) {
				logger.error(message, e);
			}
		}
	}

	public static org.slf4j.Logger getLogger(Class<?> clazz) {
		return LoggerFactory.getLogger(clazz);
	}

	public static boolean isEnabled(Class<?> clazz, int level) {
		boolean enabled = false;
		org.slf4j.Logger logger = getLogger(clazz);
		if (ValidateCommons.isNotEmpty(logger)) {
			if (LocationAwareLogger.TRACE_INT == level) {
				enabled = logger.isTraceEnabled();
			} else if (LocationAwareLogger.DEBUG_INT == level) {
				enabled = logger.isDebugEnabled();
			} else if (LocationAwareLogger.INFO_INT == level) {
				enabled = logger.isInfoEnabled();
			} else if (LocationAwareLogger.WARN_INT == level) {
				enabled = logger.isWarnEnabled();
			} else if (LocationAwareLogger.ERROR_INT == level) {
				enabled = logger.isErrorEnabled();
			}
		}
		return enabled;
	}

}
